<template>
	<!-- 课程详情 -->
	<view class="page">
		<image class="head_img" v-if="courseDetail.mainImages" :src="vuex_imgUrl+courseDetail.mainImages" />
		<view class="top">
			<view class="title">
				{{courseDetail.title}}
			</view>
			<view class="look">
				<view class="look_num">
					<u-icon name="eye" color="#999999" size="32rpx"></u-icon>
					{{courseDetail.viewNum>10000?(courseDetail.viewNum/10000).toFixed(1)+'w':courseDetail.viewNum}}
				</view>
				<view class="star_img" @click="collectionFn">
					<image v-show="courseDetail.collection" class="star" src="/static/tabbar/star_a.png" />
					<image v-show="!courseDetail.collection" class="star" src="/static/tabbar/star.png" />
				</view>
			</view>
		</view>

		<view class="tabs">
			<view class="tab_item" :class="{tab_item_a:selectTab=='1'}" @click="changeTab('1')">
				课程介绍
				<view class="tab_line"></view>
			</view>
			<view class="division"></view>
			<view class="tab_item" :class="{tab_item_a:selectTab=='2'}" @click="changeTab('2')">
				培训资源
				<view class="tab_line"></view>
			</view>
		</view>

		<view class="parse" v-show="selectTab=='1'">
			<u-parse :content="courseDetail.intro"></u-parse>
		</view>
		<view class="list" v-show="selectTab=='2'">
			<view class="list_item" v-for="(v,i) in courseDetail.words" :key="i">
				<view class="left">
					<image class="icon" src="/static/tabbar/w_icon.png" />
					<view class="name">{{v.name}}</view>
				</view>
				<view class="right" @click="check(1,v.url)">
					查看
				</view>
			</view>
			<view class="list_item" v-for="(v,i) in courseDetail.excels" :key="i">
				<view class="left">
					<image class="icon" src="/static/tabbar/x_icon.png" />
					<view class="name">{{v.name}}</view>
				</view>
				<view class="right" @click="check(2,v.url)">
					查看
				</view>
			</view>
			<view class="list_item" v-for="(v,i) in courseDetail.images" :key="i">
				<view class="left">
					<image class="icon" src="/static/tabbar/j_icon.png" />
					<view class="name">{{v.name}}</view>
				</view>
				<view class="right" @click="check(3,v.url)">
					查看
				</view>
			</view>
		</view>
		<liu-drag-button @clickBtn="clickBtn">
			<image class="share_icon" src="/static/tabbar/share_icon.png" mode=""></image>
		</liu-drag-button>

		<!-- 会员弹出层 -->
		<u-popup :show="show" mode="center" bgColor="transparent">
			<view class="member_bg">
				<view class="title">开通会员即可免费查看</view>
				<image class="diamonds" src="/static/tabbar/diamonds.png" />
				<view class="btns">
					<view class="btn" @click="show=false">取消</view>
					<view class="btn btn1" @click="$fn.jumpPage('/pages/my/pages/memberCenter')">立即开通</view>
				</view>
			</view>
		</u-popup>
		<c-share ref="share" />
	</view>
</template>

<script>
	export default {
		name: "courseDetails",
		data() {
			return {
				show: false,
				selectTab: '1',
				content: '',
				courseDetail: {
					words: '',
					excels: '',
					images: '',
					collection: false
				},
				isVip: 'false'
			};
		},
		onLoad(option) {
			this.getCourseDetailFn(option.courseId)
		},
		onShow() {
			this.getIsVipFn()
		},
		methods: {
			getCourseDetailFn(courseId) {
				this.$api.getCourseDetail({
					courseId
				}).then(res => {
					console.log('课程详情', res);
					if (res.data.words) {
						res.data.words = JSON.parse(res.data.words)
					}
					if (res.data.excels) {
						res.data.excels = JSON.parse(res.data.excels)
					}
					if (res.data.images) {
						res.data.images = JSON.parse(res.data.images)
					}
					this.courseDetail = res.data
				})
			},
			// 判断用户是否是会员
			getIsVipFn() {
				this.$api.getIsVip().then(res => {
					this.isVip = res.data
					console.log('判断用户是否是会员', res);
				})
			},

			// 小程序课程收藏-取消收藏
			collectionFn() {
				this.$api.collection({
					courseId: this.courseDetail.id
				}).then(res => {
					this.courseDetail.collection = !this.courseDetail.collection
					// console.log('收藏成功', res);
					// this.getCourseDetailFn(this.courseDetail.id)
				})
			},


			// 切换tab
			changeTab(i) {
				this.selectTab = i
			},
			// 查看
			check(type, url) {
				if (this.isVip == "false") {
					this.show = true
				} else {
					if (type == 1 || type == 2) {
						console.log('打开文档', this.vuex_imgUrl + url);

						uni.downloadFile({
							url: this.vuex_imgUrl + url,
							success: function(res) {
								var filePath = res.tempFilePath;
								uni.openDocument({
									filePath: filePath,
									success() {
										console.log('文件打开成功');
									},
									fail(err) {
										console.log('文件打开失败', err);
									}
								});
							}
						});
					} else {
						uni.previewImage({
							urls: [this.vuex_imgUrl + url]
						})
					}
				}
			},
			// 去分享
			clickBtn() {
				console.log('去分享');
				this.$refs.share.getSharePosterFn()
			},
		}
	};
</script>

<style lang="scss" scoped>
	.head_img {
		width: 750rpx;
		height: 563rpx;
	}

	.top {
		padding: 28rpx 32rpx;
		box-sizing: border-box;

		.title {
			font-weight: bold;
			font-size: 32rpx;
			color: #1A1A1A;
		}

		.look {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 18rpx;

			.look_num {
				display: flex;
				align-items: center;
				gap: 10rpx;
				font-weight: 400;
				font-size: 24rpx;
				color: #999999;
			}

			.star_img {
				width: 40rpx;
				height: 38rpx;

				.star {
					width: 40rpx;
					height: 38rpx;
				}
			}


		}

	}

	.tabs {
		border-top: 1rpx solid #E6E6E6;
		border-bottom: 1rpx solid #E6E6E6;
		display: flex;
		padding-top: 28rpx;

		.tab_item {
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 22rpx;
			font-size: 28rpx;
			color: #666666;


			.tab_line {
				width: 56rpx;
				height: 8rpx;
			}
		}

		.tab_item_a {
			font-weight: bold;
			font-size: 28rpx;
			color: #1A1A1A;

			.tab_line {
				background: #EF4630;
			}
		}

		.division {
			width: 1rpx;
			height: 32rpx;
			background: #B3B3B3;
		}
	}

	.parse {
		padding: 28rpx 32rpx;
		box-sizing: border-box;
	}

	.list {
		padding: 0 32rpx;
		box-sizing: border-box;

		.list_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			gap: 20rpx;
			padding: 24rpx 0;
			border-bottom: 1rpx solid #E6E6E6;

			.left {
				display: flex;
				align-items: center;
				gap: 16rpx;

				.icon {
					width: 36rpx;
					height: 40rpx;
				}

				.name {
					flex: 1;
					font-size: 28rpx;
					color: #666666;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 1;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.right {
				width: 88rpx;
				height: 44rpx;
				background: linear-gradient(141deg, #F0A367 0%, #EF4630 100%);
				border-radius: 30rpx;
				font-size: 24rpx;
				color: #FFFFFF;
				text-align: center;
				line-height: 44rpx;
			}
		}
	}

	.share_icon {
		width: 80rpx;
		height: 96rpx;
	}


	.member_bg {
		width: 520rpx;
		height: 580rpx;
		background: linear-gradient(180deg, #F5E3CB 0%, #FDFDFD 100%);
		border-radius: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;


		.title {
			font-weight: 800;
			font-size: 40rpx;
			line-height: 47rpx;
			background: linear-gradient(to right, #666666 0%, #1A1A1A 100%);
			-webkit-background-clip: text;
			-webkit-text-fill-color: transparent;
			margin: 48rpx 0 76rpx;
		}

		.diamonds {
			width: 320rpx;
			height: 200rpx;
		}

		.btns {
			display: flex;
			align-items: center;
			gap: 24rpx;
			margin-top: 80rpx;

			.btn {
				width: 200rpx;
				height: 80rpx;
				background: #595959;
				border-radius: 12rpx;
				opacity: 0.9;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 80rpx;
				text-align: center;
			}

			.btn1 {
				background: linear-gradient(91deg, #4D4D4D 0%, #1A1A1A 100%);
			}
		}
	}
</style>